package com.example.threadmodel.model;

import lombok.extern.slf4j.Slf4j;

/**
 * 负责启动
 */
@Slf4j
public class StartUpQueueTask implements ShortTimeRunnable {
    private final ActionQueue actionQueue;

    public StartUpQueueTask(ActionQueue actionQueue) {
        this.actionQueue = actionQueue;
    }

    @Override
    public void run() {
        // 此处run的判断，与submitNextAction中对run的修改，应当加锁互斥
        if (actionQueue.isRunning()) {
            long now = System.currentTimeMillis();
            // 上次submit在十分钟之前，而当前又是run=true；应该是假run,出了问题，这次强制submit
            if (actionQueue.getLastSubmitActTime() < now - 10 * 60 * 1000) {
                runAgain(now);
            }
        } else {
            actionQueue.submitAction();
        }
    }

    private void runAgain(long now) {
        log.error("ACTION ERROR:lastSubmitActTime:{}, now:{} seem like fake run,queue rerun now", actionQueue.getLastSubmitActTime(), now);
        actionQueue.submitAction();
    }
}
